package com.java2405.first.luoyang_management.effective_population_management.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java2405.first.luoyang_management.common.CommonResult;
import com.java2405.first.luoyang_management.effective_population_management.entity.FpOtherInformation;
import com.java2405.first.luoyang_management.effective_population_management.entity.Manager;
import com.java2405.first.luoyang_management.effective_population_management.mapper.ManagerMapper;
import com.java2405.first.luoyang_management.effective_population_management.service.impl.ManagerServiceImpl;
import com.java2405.first.luoyang_management.jwt.utils.JwtUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <manager>
 *  管理员 前端控制器
 * </manager>
 *
 * @author zzh
 * @since 2025-03-14
 */
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Resource
    ManagerServiceImpl managerService;

    @GetMapping("/login")
    public Map<String,Object> login(String account,String password) {
        Manager manager = managerService.getManager(account, password);
        Map<String, Object> map = new HashMap<>();
        try{
            Map<String,String> payload = new HashMap<>();
            if (manager != null){
                payload.put("id",manager.getId()+"");
                payload.put("account",manager.getAccount());
                payload.put("password",manager.getPassword());
                // ⽣成JWT的令牌
                String token = JwtUtils.createToken(payload);
                map.put("state",true);
                map.put("msg","认证成功");
                // 响应token
                map.put("token",token);

            }

        }catch (Exception e){
            map.put("state",false);
            map.put("msg",e.getMessage());
        }

        return map;
    }
    @GetMapping("/getInfo")
    public CommonResult<Manager> getInfo() {
        try {
            return CommonResult.success(managerService.getById(1));
        }catch (Exception e){
            return CommonResult.failed(e.getMessage());
        }
    }

}
